package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.constant.CommodityLogConstants;
import com.ruoyi.common.core.utils.QiniuUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysCommodity;
import com.ruoyi.system.domain.SysCommodityLog;
import com.ruoyi.system.mapper.SysCommodityMapper;
import com.ruoyi.system.service.ISysCommodityLogService;
import com.ruoyi.system.service.ISysCommodityService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysCommodityServiceImpl implements ISysCommodityService {

    @Autowired
    private SysCommodityMapper sysCommodityMapper;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysCommodityLogService iSysCommodityLogService;
//    @Autowired
//    RedisService redisService;

    @Override
    public List<SysCommodity> selectCommodityList(SysCommodity sysCommodity) {

        List<SysCommodity> list = sysCommodityMapper.selectCommodityList(sysCommodity);
        for (SysCommodity Commodity : list) {
            Commodity.setCreatedSysUser(iSysUserService.selectUserById(Commodity.getCreatedby()));
            Commodity.setModifySysUser(iSysUserService.selectUserById(Commodity.getModifyby()));
        }
        SysCommodityLog sysCommodityLog = new SysCommodityLog();

        sysCommodityLog.setMethodName("selectCommodityList");
        sysCommodityLog.setOperateMan(SecurityUtils.getUsername());
        sysCommodityLog.setCommodityStatus(CommodityLogConstants.SELECT);
        sysCommodityLog.setNote(CommodityLogConstants.SELECT_EXPLAIN);
        iSysCommodityLogService.insertSysCommodityLog(sysCommodityLog);
        return list;
    }

    @Override
    public void deleteCommodityById(Long id) {
        SysCommodity commodity = sysCommodityMapper.selectCommodityById(id);
        QiniuUtils.deleteFileFromQiniu(commodity.getProductimages());
        sysCommodityMapper.deleteCommodityById(id);

        SysCommodityLog commodityLog = new SysCommodityLog();
        commodityLog.setCommodityId(commodity.getCommoditycode());
        commodityLog.setMethodName("deleteCommodityById");
        commodityLog.setOperateMan(SecurityUtils.getUsername());
        commodityLog.setCommodityStatus(CommodityLogConstants.DELETE);
        commodityLog.setNote(CommodityLogConstants.DELETE_EXPLAIN);
        iSysCommodityLogService.insertSysCommodityLog(commodityLog);
    }

    @Override
    public int updateCommodity(SysCommodity sysCommodity) {
        SysCommodity commodity = selectCommodityById(sysCommodity.getId());
        if (commodity != null) QiniuUtils.deleteFileFromQiniu(commodity.getProductimages());

        sysCommodity.setModifydate(new Date());
        int result = sysCommodityMapper.updateCommodity(sysCommodity);

        SysCommodityLog sysCommodityLog = new SysCommodityLog();
        sysCommodityLog.setMethodName("updateCommodity");
        sysCommodityLog.setCommodityId(sysCommodity.getCommoditycode());
        sysCommodityLog.setOperateMan(SecurityUtils.getUsername());
        sysCommodityLog.setCommodityStatus(CommodityLogConstants.UPDATE);
        sysCommodityLog.setNote(CommodityLogConstants.UPDATE_EXPLAIN);
        iSysCommodityLogService.insertSysCommodityLog(sysCommodityLog);
        return result;
    }

    @Override
    public SysCommodity selectCommodityById(Long id) {
        SysCommodity sysCommodity = sysCommodityMapper.selectCommodityById(id);

        SysCommodityLog sysCommodityLog = new SysCommodityLog();
        sysCommodityLog.setMethodName("selectCommodityById");
        sysCommodityLog.setOperateMan(SecurityUtils.getUsername());
        sysCommodityLog.setCommodityId(sysCommodity.getCommoditycode());
        sysCommodityLog.setCommodityStatus(CommodityLogConstants.INSERT);
        sysCommodityLog.setNote(CommodityLogConstants.INSERT_EXPLAIN);
        iSysCommodityLogService.insertSysCommodityLog(sysCommodityLog);
        return sysCommodity;
    }

    @Override
    public int insertCommodity(SysCommodity sysCommodity) {
        sysCommodity.setCreationdate(new Date());
        int result = sysCommodityMapper.insertCommodity(sysCommodity);

        SysCommodityLog sysCommodityLog = new SysCommodityLog();
        sysCommodityLog.setMethodName("insertCommodity");
        sysCommodityLog.setOperateMan(SecurityUtils.getUsername());
        sysCommodityLog.setCommodityStatus(CommodityLogConstants.INSERT);
        sysCommodityLog.setNote(CommodityLogConstants.INSERT_EXPLAIN);
        iSysCommodityLogService.insertSysCommodityLog(sysCommodityLog);
        return result;
    }
}
